﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace CalcSharp.Core.General
{

    public class RSSItem
    {

        private string link;
        private string title;
        private string description;
        private DateTime pubdate;

        public RSSItem(string Link, string Title, string Description, DateTime Pupdate)
        {
            this.link = Link;
            this.title = Title;
            this.description = Description;
            this.pubdate = Pupdate;
        }

        public string Link
        {
            get { return this.link; }
        }
        public string Title
        {
            get { return this.title; }
        }
        public string Description
        {
            get { return this.description; }
        }
        public DateTime PubDate
        {
            get { return this.pubdate; }
        }

    }

    public class RSSReader : IDisposable
    {
        private XmlTextReader rssReader;
        private XmlDocument rssDoc;
        private XmlNode nodeRss;
        private XmlNode nodeChannel;
        private XmlNode nodeItem;

        private string feedurl;
        private string c_title;
        private string c_link;
        private string c_descript;
        private string c_language;

        private List<RSSItem> items;
        private bool ok;

        public RSSItem this[int i]
        {
            get
            {
                return this.items[i];
            }
        }
        public string FeedURL
        {
            get { return this.feedurl; }
        }
        public string ChannelTitle
        {
            get { return this.c_title; }
        }
        public string ChannelDescription
        {
            get { return this.c_descript; }
        }
        public string ChannelLanguage
        {
            get { return this.c_language; }
        }
        public string ChannelLink
        {
            get { return this.c_link; }
        }
        public int Count
        {
            get { return this.items.Count; }
        }

        public RSSReader(string url)
        {
            this.feedurl = url;

            items = new List<RSSItem>();
            rssReader = new XmlTextReader(url);
            rssDoc = new XmlDocument();
            rssDoc.Load(rssReader);
            ok = false;
            RSSItem temp;
            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
            {
                if (rssDoc.ChildNodes[i].Name == "rss")
                {
                    nodeRss = rssDoc.ChildNodes[i];
                    ok = true;
                    break;
                }
            }
            if (!ok) throw new CSException("Invalid RSS Feed, No RSS element found in the document");

            ok = false;

            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
            {
                if (nodeRss.ChildNodes[i].Name == "channel")
                {
                    nodeChannel = nodeRss.ChildNodes[i];
                    ok = true;
                    break;
                }
            }

            if (!ok) throw new CSException("Invalid RSS Feed, No channel element found in the document");

            this.c_title = nodeChannel["title"].InnerText;
            this.c_descript = nodeChannel["description"].InnerText;
            this.c_link = nodeChannel["link"].InnerText;
            this.c_language = nodeChannel["language"].InnerText;

            for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
            {
                if (nodeChannel.ChildNodes[i].Name == "item")
                {
                    nodeItem = nodeChannel.ChildNodes[i];
                    //RSSItem(string Link, string Title, string Description, DateTime Pupdate)
                    temp = new RSSItem(nodeItem["link"].InnerText, nodeItem["title"].InnerText, nodeItem["description"].InnerText, DateTime.Parse(nodeItem["pubDate"].InnerText));
                    items.Add(temp);
                }
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.items != null)
                {
                    this.items.Clear();
                    this.items = null;
                }
                if (this.rssReader != null)
                {
                    this.rssReader.Close();
                    this.rssReader = null;
                }
                if (this.rssDoc != null) this.rssDoc = null;
                if (this.nodeRss != null) this.nodeRss = null;
                if (this.nodeChannel != null) this.nodeChannel = null;
                if (this.nodeItem != null) this.nodeItem = null;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
